package ch.sv7.tool.dbmanager.db.fixbuilder;

import ch.sv7.tool.dbmanager.Context;
import ch.sv7.tool.dbmanager.db.dialect.Dialect;
import ch.sv7.tool.dbmanager.db.difference.AbstractIndexDifference;
import ch.sv7.tool.dbmanager.db.difference.SchemaDifference;
import ch.sv7.tool.dbmanager.db.fix.CreateIndexFix;
import ch.sv7.tool.dbmanager.db.fix.DropIndexFix;
import ch.sv7.tool.dbmanager.db.fix.SchemaFix;

/**
 * TODO: Document me!
 *
 * @author svt
 *
 */
public class IndexParameterFixBuilder implements FilterFixBuilder {

	private String targetIndexIdentifier;
	
	public SchemaFix[] createFixes(SchemaDifference difference, Dialect dialect, Context context) {
		AbstractIndexDifference indexDifference = (AbstractIndexDifference)difference;
		SchemaFix dropFix = new DropIndexFix(indexDifference.getActualIndex());
		SchemaFix createFix = new CreateIndexFix(indexDifference.getExpectedIndex());	
		targetIndexIdentifier = createFix.getActionTarget().getSchemaIdentifier();
		return new SchemaFix[]{ dropFix, createFix };
	}

	public boolean ignoreFix(SchemaFix fix) {
		String identifier = fix.getActionTarget().getSchemaIdentifier();
		/*
		 * Remove all editions on indexes that will be dropped
		 */
		if (identifier.equals(targetIndexIdentifier) && fix.getAction() == SchemaFix.ACTION_EDIT){
			return true;
		} else {
			return false;
		}
	}

}
